package com.hundsun.cprs.yyt.gateway.util;

import java.util.ArrayList;
import java.util.List;

import com.hundsun.cprs.yyt.common.enums.EnumAreaCode;
import com.hundsun.cprs.yyt.gateway.vo.GoodsExtBaseVo;
import com.hundsun.cprs.yyt.gateway.vo.GoodsImageVo;
import com.hundsun.cprs.yyt.gateway.vo.GoodsInfoVo;
import com.hundsun.cprs.yyt.gateway.vo.GoodsSpecsVo;
import com.hundsun.cprs.yyt.service.goods.domain.GoodsImage;
import com.hundsun.cprs.yyt.service.goods.domain.GoodsSpecs;
import com.hundsun.cprs.yyt.service.goods.dto.GoodsInfo;
import com.hundsun.cprs.yyt.service.goods.service.GoodsService;

public class VoBeanUtils {
    
    public static GoodsInfoVo copyPropertiesFromExt(GoodsInfo goodsInfo){
        GoodsInfoVo temp = new GoodsInfoVo();
        copyPropertiesInner(temp,goodsInfo,true);
        return temp;
    }
    
    public static GoodsExtBaseVo copyPropertiesFrom(GoodsInfo goodsInfo){
        GoodsExtBaseVo temp = new GoodsExtBaseVo();
        copyPropertiesInner(temp,goodsInfo,true);
        return temp;
    }
    
    private static void copyPropertiesInner(GoodsExtBaseVo target,GoodsInfo source,boolean setSku){
        target.setId(source.getId());
        target.setGoodsName(source.getGoodsName());
        target.setGoodsCode(source.getGoodsCode());
        target.setGoodsDetail(source.getGoodsDetail());
        target.setGoodsPrice(source.getGoodsPrice() != null?source.getGoodsPrice().longValue():null);
        target.setCostPrice(source.getCostPrice() != null?source.getCostPrice().longValue():null);
        target.setIntegralRewardRate(source.getIntegralRewardRate());
        target.setQuotaRewardRate(source.getQuotaRewardRate());
        if (source.getAttributeList() != null) {
            target.setAttributeList(source.getAttributeList().replace('"', '\\'));
        } else {
            target.setAttributeList("");
        }
        target.setClassifyId(source.getClassifyId());
        target.setOrganizationId(source.getOrganizationId());
        target.setOrganizationName(source.getOrganizationName());
        target.setStatus(source.getStatus());
        target.setIsDeleted(source.getIsDeleted());
        target.setTradeUnit(source.getTradeUnit());
        target.setPublishTime(source.getPublishTime());
        target.setGmtCreate(source.getGmtCreate());
        target.setGmtModified(source.getGmtModified());
        
        if (source.getImageList() != null && !source.getImageList().isEmpty()) {
            target.setGoodsImageVoList(new ArrayList<GoodsImageVo>(source.getImageList().size()));
            for(GoodsImage image : source.getImageList()){
                GoodsImageVo imageVo = new GoodsImageVo();
                imageVo.setBigImageUrl(image.getBigImageUrl());
                imageVo.setOriginalImageUrl(image.getOriginalImageUrl());
                imageVo.setSmallImageUrl(image.getSmallImageUrl());
                imageVo.setImageType(image.getImageType());
                target.getGoodsImageVoList().add(imageVo);
            }
        }
        
        if (setSku && source.getSkus() != null && !source.getSkus().isEmpty()) {
            target.setGoodsSpecsVoList(new ArrayList<GoodsSpecsVo>(source.getSkus().size()));
            for(GoodsSpecs goodsSpecs: source.getSkus()){
                GoodsSpecsVo skuVo = new GoodsSpecsVo();
                skuVo.setAvailableStock(goodsSpecs.getAvailableStock());
                skuVo.setGoodsId(goodsSpecs.getGoodsId());
                skuVo.setGoodsPrice(goodsSpecs.getGoodsPrice());
                if (goodsSpecs.getGoodsSpecs() != null) {
                    skuVo.setGoodsSpecs(goodsSpecs.getGoodsSpecs().replace('"', '\\'));
                }
                skuVo.setGoodsStock(goodsSpecs.getGoodsStock());
                skuVo.setPlateCode(goodsSpecs.getPlateCode());
                skuVo.setQuotaAmount(goodsSpecs.getQuotaAmount());
                skuVo.setId(goodsSpecs.getId());
                target.getGoodsSpecsVoList().add(skuVo);
            }
        }
    }
    
    public static List<GoodsExtBaseVo> copyPropertiesFrom(List<GoodsInfo> goodsInfoList){
        List<GoodsExtBaseVo> target = new ArrayList<GoodsExtBaseVo>();
        for(GoodsInfo goodsInfo : goodsInfoList){
            GoodsExtBaseVo temp = new GoodsExtBaseVo();
            copyPropertiesInner(temp,goodsInfo,true);
            target.add(temp);
        }
        return target;
    }
}
